package ani.trainning.cacobatph.das;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.ServiceConnection;
import org.ksoap2.transport.ServiceConnectionSE;
import org.kxml2.kdom.Element;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import ani.trainning.cacobatph.ConstantUtil;
import ani.trainning.cacobatph.R;
import ani.trainning.cacobatph.utilities;

public class Thr10_ValidationDAS {

	private final Handler mHandler;
	private final Context context;
	public final static int SEND_DAS_OK=200;
	public final static int SEND_DAS_FAILURE=-200;

	String user;
	String pass;
	String entete;
	String lignesDas;

	// constructor
	public Thr10_ValidationDAS(Handler handler,
			Context context, String _user, String _pass, String entete,String lignesDas) {
		mHandler = handler;
		this.context = context;
		this.user = _user;
		this.pass = _pass;
		this.entete = entete;
		this.lignesDas=lignesDas;
	}

	public Thread createThread() {
		return new Thread() {
			@Override
			public void run() {
				// creation du message.
				try {

					String response = validate(context, user, pass);

					// Recuperation de la reponse et l'envoi au handler.
					Message msg = new Message();
					msg.what = SEND_DAS_OK;
					msg.obj = response;
					mHandler.sendMessage(msg);

				} catch (Exception e) {
					Message msg = new Message();
					msg.obj = e;
					if(e instanceof IOException)
						msg.what = ConstantUtil.EXCEPTION_IO2;
					else
						msg.what =SEND_DAS_FAILURE;
					Log.e("thread exeption : ", "" + e.getMessage());
					mHandler.sendMessage(msg);

				}

			}
		};
	}

	private String validate(final Context context, String userName,
			String passWord) throws Exception {

		SoapObject request = new SoapObject(context.getResources().getString(
				R.string.NAMESPACE), context.getResources().getString(
						R.string.DAS_SAISIE));

		PropertyInfo param = new PropertyInfo();
		param.setName("_entete");
		param.setType(String.class);
		param.setValue(entete);
		request.addProperty(param);
		Log.i("chargement", "entete :"+ entete);

		param = new PropertyInfo();
		param.setName("_lignesDas");
		param.setType(String.class);
		param.setValue(lignesDas);
		request.addProperty(param);
		Log.i("chargement", "lignesDas :"+ lignesDas);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);

		envelope.headerOut = new Element[1];
		envelope.headerOut[0] = utilities.buildAuthHeader(context, userName, passWord);

		HttpTransportSE androidHttpTransport = new
		HttpTransportSE(context.getResources().getString(R.string.URL))
		{
			@Override
			protected ServiceConnection getServiceConnection()
			throws IOException {
				return new
				ServiceConnectionSE(context.getResources().getString(R.string.URL),
						ConstantUtil.TIMEOUT);
			}
		};
		androidHttpTransport.call(context.getResources().getString(R.string.NAMESPACE)
				+
				context.getResources().getString(R.string.DAS_SAISIE),
				envelope, null);

		String result = String.valueOf(envelope.getResponse());
		Log.i("chargement", "result : " + result);
		return result;

	}

}
